<template>
  <el-container v-loading="cloading">
    <el-header v-if="menuTree.length">
      当前操作角色：{{ currentAuditData.Name }}
      <el-button type="primary" @click="onSave">保存</el-button>
    </el-header>
    <el-main class="nopadding">
      <el-tree
        ref="menu"
        node-key="id"
        :data="menuTree"
        :props="defaultProps"
        highlight-current
        empty-text="点击操作查看角色菜单权限"
      >
        <template #default="{ node, data }">
          <div class="custom-tree-node">
            <span class="label">{{ node.label }}</span>
            <div class="tree-node-checkbox">
              <div v-for="item in data.functions">
                <el-checkbox
                  :key="item.id"
                  v-if="item.isCan"
                  v-model="item.isSelect"
                >
                  {{ item.title }}
                </el-checkbox>
              </div>
            </div>
          </div>
        </template>
      </el-tree>
    </el-main>
  </el-container>
</template>
<script setup>
import { ElMessage } from 'element-plus';
import { getCurrentInstance, ref, nextTick, unref } from 'vue';
import roleMenuFunctionApi from '@/api/rolemenufunction/index';

// 当前用户信息
const currentAuditData = ref({});
// loading
const cloading = ref(false);

// 菜单树配置
const defaultProps = {
  children: 'children',
  label: 'title',
};
const menu = ref();
// 菜单树++
const menuTree = ref([]);

// 根据 roleId 获取树
const getTree = async (id) => {
  cloading.value = true;
  const res = await roleMenuFunctionApi.getRoleMenuFunctionsById.post(id);
  cloading.value = false;
  if (res.code !== 0) {
    return ElMessage.warning('获取数据失败');
  }
  menuTree.value = res.data;
  // checkedMenu.value = res.data.selectMenu
};

var saveTreeList = [];
//过滤菜单父子树，过滤成后端要的请求参数
var saveTreeFliter = (menuTree) => {
  menuTree.forEach((item) => {
    if (item.children.length <= 0 && item.functions.length > 0) {
      var functionList = [];
      item.functions.forEach((funItem) => {
        if (funItem.isSelect) {
          var functionItem = funItem.id;
          functionList.push(functionItem);
        }
      });
      var menuFunctionItem = {
        menuId: item.id,
        functionIds: functionList,
      };
      saveTreeList.push(menuFunctionItem);
    } else {
      saveTreeFliter(item.children);
    }
  });
  var menuFunctionSave = {
    roleId: currentAuditData.value.Id,
    menuFunctionList: saveTreeList,
  };
  return menuFunctionSave;
};
// 提交更新后的 tree 给服务器
const onSave = async () => {
  cloading.value = true;
  var menuFunctionSave = saveTreeFliter(menuTree.value);

  const res = await roleMenuFunctionApi.save.post(menuFunctionSave);
  cloading.value = false;
  if (res.code !== 0) {
    return ElMessage.warning('操作失败');
  }
  ElMessage.success('更新权限成功');
};

// 从父组件获取当前用户信息
const setData = async (data) => {
  currentAuditData.value = data;
};

// 暴露子组件方法给父组件调用
defineExpose({
  getTree,
  setData,
});
</script>
<style lang="scss" scoped>
.el-main {
  padding: 0 !important;
}
.el-tree {
  width: 100%;
  :deep(.el-tree-node__content) {
    min-height: 50px;
  }
  .custom-tree-node {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    // padding-right: 24px;
    height: 100%;
    .label {
      display: flex;
      align-items: center;
      height: 100%;
      color: rgb(15, 109, 185);
    }
    .tree-node-checkbox {
      display: flex;
      margin-left: 10px;
      justify-content: flex-end;
      :deep(.el-checkbox) {
        width: 60px;
      }
    }
  }
}
</style>
